package com.hskn.hss.module.employeePunchCard.service.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hskn.hss.core.utils.uuid.IdUtils;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.module.employeePunchCard.entity.EmployeePunchCard;
import com.hskn.hss.module.employeePunchCard.mapper.EmployeePunchCardMapper;
import com.hskn.hss.module.employeePunchCard.service.EmployeePunchCardService;
import com.hskn.hss.module.grid.entity.Grid;
import com.hskn.hss.module.grid.service.IGridService;
import com.hskn.hss.module.gridBindCheckWork.entity.GridBindCheckwork;
import com.hskn.hss.module.gridBindCheckWork.service.GridBindCheckWorkService;
import com.hskn.hss.module.kfkdeal.entity.car.KfkHandleParam;
import com.hskn.hss.module.kfkdeal.to.KfkDealEmployeeTo;
import com.hskn.hss.module.tlcar.entity.TlCar;
import com.hskn.hss.module.tlemployee.entity.TlEmployee;
import com.hskn.hss.utils.DistanceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Objects;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Service
public class EmployeePunchCardServiceImpl extends ServiceImpl<EmployeePunchCardMapper, EmployeePunchCard> implements EmployeePunchCardService {
    @Autowired
    GridBindCheckWorkService gridBindCheckWorkService;
    @Autowired
    ISysDepartService departService;
    @Autowired
    IGridService gridService;

//    /**
//     * 人员打卡
//     *
//     * @param kfkDealEmployeeTo
//     * @param tlEmployee
//     * @throws
//     */
//    @Override
//    public void punchCard(KfkDealEmployeeTo kfkDealEmployeeTo, TlEmployee tlEmployee) {
//        // 判断是否在工作时间内 进入网格  打卡
//        if (null != tlEmployee) {
//            GridBindCheckwork gridBindCheckwork = getGridBindCheckwork(tlEmployee.getId());
//            if (Objects.isNull(gridBindCheckwork) || (Objects.nonNull(gridBindCheckwork) && gridBindCheckwork.getUpStatus().equals(2))) {
//                return;
//            }
//            Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<SysDepart>());
//            Grid grid = gridService.getById(gridBindCheckwork.getGridId());
//            Boolean flag = DistanceUtils.insideGrid(Double.valueOf(kfkDealEmployeeTo.getLng()), Double.valueOf(kfkDealEmployeeTo.getLat()), grid);
//            Date startDate = gridBindCheckwork.getStartTime();
//            Date endDate = gridBindCheckwork.getEndTime();
//            Date nowDate = kfkDealEmployeeTo.getDate();
//            //允许提前打卡
//            Long punchCardTime = new BigDecimal(startDate.getTime()).subtract(new BigDecimal(grid.getPunchCard()).multiply(new BigDecimal(60000))).longValue();
//            Date punchCardDate = new Date(punchCardTime);
//            //迟到
//            Long laterInTime = new BigDecimal(startDate.getTime()).add(new BigDecimal(grid.getLaterIn()).multiply(new BigDecimal(60000))).longValue();
//            Date laterInDate = new Date(laterInTime);
//            //早退
//            Long leaveEarlyTime = new BigDecimal(gridBindCheckwork.getEndTime().getTime()).subtract(new BigDecimal(grid.getLeaveEarly()).multiply(new BigDecimal(60000))).longValue();
//            Date leaveEarlyDate = new Date(leaveEarlyTime);
//            //旷工
//            Long noWorkTime = new BigDecimal(startDate.getTime()).add(new BigDecimal(grid.getNoWork()).multiply(new BigDecimal(60000))).longValue();
//            Date noWorkDate = new Date(noWorkTime);
//            EmployeePunchCard employeePunchCard = new EmployeePunchCard();
//
//            QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
//            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getGridBindCheckworkId, gridBindCheckwork.getId());
//            employeePunchCardQueryWrapper.lambda().last(" limit 1");
//            EmployeePunchCard employeePunchCard1 = getOne(employeePunchCardQueryWrapper);
//            if (Objects.nonNull(employeePunchCard1)) {
//                employeePunchCard = employeePunchCard1;
//            } else {
//                employeePunchCard.setId(IdUtils.simpleUUID());
//                employeePunchCard.setGridBindCheckworkId(gridBindCheckwork.getId());
//            }
//            employeePunchCard.setManCarType(1);
//            employeePunchCard.setPunchCardDate(DateUtil.formatDate(kfkDealEmployeeTo.getDate()));
//            employeePunchCard.setPunchCardMonth(DateUtil.format(kfkDealEmployeeTo.getDate(), "yyyy-MM"));
//            employeePunchCard.setEmployeeId(tlEmployee.getId());
//            employeePunchCard.setGridId(grid.getGridId());
//            employeePunchCard.setEmployeeName(tlEmployee.getName());
//            employeePunchCard.setDepartmentId(tlEmployee.getEmpldeptid());
//            SysDepart sysDepart = departMap.get(tlEmployee.getEmpldeptid());
//            if (Objects.nonNull(sysDepart)) {
//                employeePunchCard.setDepartmentName(sysDepart.getDepartName());
//            }
//            employeePunchCard.setType(0);
//            Boolean insertPunchCardFlag = false;
//            if (flag) {
//                if (Objects.nonNull(gridBindCheckwork.getUpStatus()) && gridBindCheckwork.getUpStatus().equals(0)) {
//                    if (punchCardDate.before(nowDate) && nowDate.before(laterInDate)) {//正常打卡
//                        employeePunchCard.setLaterIn(0);
//                        employeePunchCard.setNoWork(0);
//                        employeePunchCard.setLackCard(0);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkDealEmployeeTo.getDate()));
//                        gridBindCheckwork.setWorkstartTime(kfkDealEmployeeTo.getDate());
//                    } else if (laterInDate.before(nowDate) && nowDate.before(noWorkDate)) {//迟到
//                        employeePunchCard.setLaterIn(1);
//                        employeePunchCard.setNoWork(0);
//                        employeePunchCard.setLackCard(0);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkDealEmployeeTo.getDate()));
//                        gridBindCheckwork.setWorkstartTime(kfkDealEmployeeTo.getDate());
//                    } else if (noWorkDate.before(nowDate) && nowDate.before(endDate)) {//旷工
//                        employeePunchCard.setLaterIn(0);
//                        employeePunchCard.setNoWork(1);
//                        employeePunchCard.setLackCard(1);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkDealEmployeeTo.getDate()));
//                        gridBindCheckwork.setWorkstartTime(kfkDealEmployeeTo.getDate());
//                    }
//                    insertPunchCardFlag = true;
//
//                    gridBindCheckwork.setUpStatus(1);
//                }
//            }
//            if (insertPunchCardFlag) {
//                saveOrUpdate(employeePunchCard);
//                gridBindCheckWorkService.updateById(gridBindCheckwork);
//            }
//        }
//    }
//
//    /**
//     * 车辆打卡
//     *
//     * @param kfkHandleParam
//     * @param tlCar
//     * @throws
//     */
//    @Override
//    public void punchCard(KfkHandleParam kfkHandleParam, TlCar tlCar) {
//        // 判断是否在工作时间内 进入网格  打卡
//        if (null != tlCar) {
//            String carId = tlCar.getId();
//            GridBindCheckwork gridBindCheckwork = getGridBindCheckwork(carId);
//            if (Objects.isNull(gridBindCheckwork) || (Objects.nonNull(gridBindCheckwork) && gridBindCheckwork.getUpStatus().equals(2))) {
//                return;
//            }
//            Map<String, SysDepart> departMap = departService.getDepartMap(new QueryWrapper<SysDepart>());
//            Grid grid = gridService.getById(gridBindCheckwork.getGridId());
//            boolean flag = DistanceUtils.insideGrid(Double.valueOf(kfkHandleParam.getCurrentCarTrack().getLng()), Double.valueOf(kfkHandleParam.getCurrentCarTrack().getLat()), grid);
//            Date startDate = gridBindCheckwork.getStartTime();
//            Date endDate = gridBindCheckwork.getEndTime();
//            Date nowDate = kfkHandleParam.getLocationDate();
//            //允许提前打卡
//            Long punchCardTime = new BigDecimal(startDate.getTime()).subtract(new BigDecimal(grid.getPunchCard()).multiply(new BigDecimal(60000))).longValue();
//            Date punchCardDate = new Date(punchCardTime);
//            //迟到
//            Long laterInTime = new BigDecimal(startDate.getTime()).add(new BigDecimal(grid.getLaterIn()).multiply(new BigDecimal(60000))).longValue();
//            Date laterInDate = new Date(laterInTime);
//            //早退
//            Long leaveEarlyTime = new BigDecimal(gridBindCheckwork.getEndTime().getTime()).subtract(new BigDecimal(grid.getLeaveEarly()).multiply(new BigDecimal(60000))).longValue();
//            Date leaveEarlyDate = new Date(leaveEarlyTime);
//            //旷工
//            Long noWorkTime = new BigDecimal(startDate.getTime()).add(new BigDecimal(grid.getNoWork()).multiply(new BigDecimal(60000))).longValue();
//            Date noWorkDate = new Date(noWorkTime);
//            EmployeePunchCard employeePunchCard = new EmployeePunchCard();
//            QueryWrapper<EmployeePunchCard> employeePunchCardQueryWrapper = new QueryWrapper<>();
//            employeePunchCardQueryWrapper.lambda().eq(EmployeePunchCard::getGridBindCheckworkId, gridBindCheckwork.getId());
//            employeePunchCardQueryWrapper.lambda().last(" limit 1");
//            EmployeePunchCard employeePunchCard1 = getOne(employeePunchCardQueryWrapper);
//            if (Objects.nonNull(employeePunchCard1)) {
//                employeePunchCard = employeePunchCard1;
//            } else {
//                employeePunchCard.setGridBindCheckworkId(gridBindCheckwork.getId());
//            }
//            employeePunchCard.setManCarType(2);
//            employeePunchCard.setPunchCardDate(DateUtil.formatDate(kfkHandleParam.getCurrentCarTrack().getTimestamp()));
//            employeePunchCard.setPunchCardMonth(DateUtil.format(kfkHandleParam.getCurrentCarTrack().getTimestamp(), "yyyy-MM"));
//            employeePunchCard.setEmployeeId(tlCar.getId());
//            employeePunchCard.setGridId(grid.getGridId());
//            employeePunchCard.setEmployeeName(tlCar.getCarnumber());
//            employeePunchCard.setDepartmentId(tlCar.getCardeptid());
//            SysDepart sysDepart = departMap.get(tlCar.getCardeptid());
//            if (Objects.nonNull(sysDepart)) {
//                employeePunchCard.setDepartmentName(sysDepart.getDepartName());
//            }
//            employeePunchCard.setType(0);
//            Boolean insertPunchCardFlag = false;
//            if (flag) {
//                if (Objects.nonNull(gridBindCheckwork.getUpStatus()) && gridBindCheckwork.getUpStatus().equals(0)) {
//                    if (punchCardDate.before(nowDate) && nowDate.before(laterInDate)) {//正常打卡
//                        employeePunchCard.setLaterIn(0);
//                        employeePunchCard.setNoWork(0);
//                        employeePunchCard.setLackCard(0);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkHandleParam.getCurrentCarTrack().getTimestamp()));
//                        gridBindCheckwork.setWorkstartTime(kfkHandleParam.getCurrentCarTrack().getTimestamp());
//                    } else if (laterInDate.before(nowDate) && nowDate.before(noWorkDate)) {//迟到
//                        employeePunchCard.setLaterIn(1);
//                        employeePunchCard.setNoWork(0);
//                        employeePunchCard.setLackCard(0);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkHandleParam.getCurrentCarTrack().getTimestamp()));
//                        gridBindCheckwork.setWorkstartTime(kfkHandleParam.getCurrentCarTrack().getTimestamp());
//                    } else if (noWorkDate.before(nowDate) && nowDate.before(endDate)) {//旷工
//                        employeePunchCard.setLaterIn(0);
//                        employeePunchCard.setNoWork(1);
//                        employeePunchCard.setLackCard(1);
//                        employeePunchCard.setWorkTime(DateUtil.formatTime(kfkHandleParam.getCurrentCarTrack().getTimestamp()));
//                        gridBindCheckwork.setWorkstartTime(kfkHandleParam.getCurrentCarTrack().getTimestamp());
//                    }
//                    insertPunchCardFlag = true;
//                    gridBindCheckwork.setUpStatus(1);
//                }
//            }
//            if (insertPunchCardFlag) {
//                saveOrUpdate(employeePunchCard);
//                gridBindCheckWorkService.updateById(gridBindCheckwork);
//            }
//        }
//    }
//
//    private GridBindCheckwork getGridBindCheckwork(String itemId) {
//        QueryWrapper<GridBindCheckwork> gridBindCheckworkQueryWrapper = new QueryWrapper<>();
//        gridBindCheckworkQueryWrapper.lambda().eq(GridBindCheckwork::getItemId, itemId);
//        gridBindCheckworkQueryWrapper.lambda().ge(GridBindCheckwork::getEndTime, new Date());
//        gridBindCheckworkQueryWrapper.lambda().orderByAsc(GridBindCheckwork::getStartTime);
//        gridBindCheckworkQueryWrapper.lambda().orderByAsc(GridBindCheckwork::getEndTime);
//        gridBindCheckworkQueryWrapper.lambda().last(" limit 1");
//        return gridBindCheckWorkService.getOne(gridBindCheckworkQueryWrapper);
//    }
}
